<?php

class Sistema_FullbannerController extends App_Controller_Action
{  
    private $_table     = 'FULLBANNER';
    private $_pk        = 'BAN_ID';
    private $_order_col = 'BAN_ORD';
    private $_order     = 'ASC';
    private $_sta_col   = 'BAN_STA';
    private $obj        = '';
    private $form       = '';
    private $_file_col  = 'BAN_FILE'; 
    public  $dir = 'public/upload/fullbanner/';
    
    public function init()
    {
        if($this->getRequest()->getActionName() == 'edit') $this->form = new Sistema_Form_Fullbanner();
    }

    public function indexAction() {
        Zend_Loader::loadClass('tools','library');
        
        $where = $this->_pk.' > 0';
        
        $query = 'SELECT SQL_CACHE * FROM '.$this->_table.' WHERE '.$where.' ORDER BY '.$this->_order_col.' '.$this->_order;
        $result = $this->db->fetchAll($query);
        $this->view->result = $this->paginacao($result);
    }
    
    
    public function editAction() {
        
        Zend_Loader::loadClass('imagem','library');
        
        $this->view->form = $this->form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if($this->form->isValid($formData)) {
                    $id = (int) $formData[$this->_pk];

                    $returnImg = imagem::upload($this->_file_col,$this->dir,$id);

                    $data = $this->form->getValues();
                    
                    unset($data['img']);
                    unset($data[$this->_file_col]);
                    unset($data['br']);
                    
                    if ($id == 0){
                        $this->db->insert($this->_table, $data);
                        $id =  $this->db->lastInsertId();                     
                        imagem::rename($id,$this->dir,$returnImg['filenew']);
                    }
                    else{
                        $this->db->update($this->_table, $data,$this->_pk.' = ' . $id);
                    }
                    
                    if(strlen($_FILES[$this->_file_col]['tmp_name']) > 1){
                        $img = glob($this->dir.$id.'.*');
                        foreach($img as $filetemp) $filename = $filetemp;
                        $ext = end(explode('.', $filename));
                        imagem::reize($filename,$this->dir.$id.'_sld.'.$ext, '100', '44'); 
                    }              

                    $this->_helper->redirector('index');
                } else {
                    $this->form->populate($formData);
                }
            } catch(Exception $ex) {
                Zend_Loader::loadClass('errordb','library');
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                // echo $ex->getMessage();
                $this->form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $data = $this->db->fetchRow('SELECT SQL_CACHE * FROM '.$this->_table.' WHERE '.$this->_pk . ' = ' . $id);
                $this->form->populate($data);
            }
        }
    }

    public function deleteAction() {
        
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        
        $ok = FALSE;
        
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {
            $ok = (bool) $this->db->delete($this->_table,$this->_pk.' = '.$id);
            
            Zend_Loader::loadClass('imagem','library');
            imagem::delete($id,$this->dir);
            
            foreach (glob($this->dir.$id.'_*.*') as $file) {
                unlink($file);
            }
            
            //$this->_helper->redirector('index'); 
            
        } 
        
        echo json_encode(array('ok' => $ok));
    }

}
